﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataParser;

namespace argusThreeAndAHalfMen
{
    public partial class frmHome : Form
    {
        public static ArrayList branchList;
        public static frmHome home;

        //FORM HOME
        public frmHome()
        {
            InitializeComponent();
            home = this;
            branchList = new Parser().ParseBranch("argus-BIG.txt"); //take content from this file, which contains the sets of data.
        }

        //BTN SELECT BRANCH CLICK EVENT
        public void btnSelectBranch_Click(object sender, EventArgs e) //when this btn is clicked, the chosen branch will be selected and the following will run.
        {//creates a string of the chosen index, if user selects a branch then a branch object is created and passed to the branch details form via pointer.
            string branch = lstBranchSearchResults.Items[lstBranchSearchResults.SelectedIndex].ToString();

            if (branch != "No matches found...")
            {
                int branchID = Convert.ToInt16(branch.Substring(branch.IndexOf("100"), 5));
                Branch chosenBranch = new Branch();

                foreach (Branch branches in branchList)
                {
                    if (branchID == branches.BranchID)
                        chosenBranch = branches;
                }
                frmHome.ActiveForm.Hide();
                frmBranchDetails tempBranchDetails = new frmBranchDetails(chosenBranch);
                tempBranchDetails.Show();

            }
        }

        //FORM HOME LOAD EVENT
        public void frmHome_Load(object sender, EventArgs e)
        {//loads branches
            foreach (Branch branch in branchList)
                lstBranchSearchResults.Items.Add(branch.Nickname + "  ( " + branch.BranchID + " )");
            lstBranchSearchResults.Sorted = true;
        }

        //TEXT BOX BRANCH SEARCH TEXT CHANGED EVENT
        private void txtBranchSearch_TextChanged(object sender, EventArgs e)
        {//on text changed, searchbox is updated to relate searchbox entry to all applicable matches
            lstBranchSearchResults.Items.Clear();
            var searchVal = txtBranchSearch.Text;

            foreach (Branch branch in branchList)
            {//search branchlist for related items
                if (branch.BranchID.ToString().IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) >= 0 || branch.Nickname.IndexOf(searchVal, StringComparison.OrdinalIgnoreCase) >= 0)
                    lstBranchSearchResults.Items.Add(branch.Nickname + "  ( " + branch.BranchID + " )");
                lstBranchSearchResults.Sorted = true;
            }

            while (txtBranchSearch.Text != "Type to search...")
            {
                if (lstBranchSearchResults.Items.Count == 0 && txtBranchSearch.Text != null)
                {
                    lstBranchSearchResults.Items.Add("No matches found...");
                }

                break;
            }
        }

        //TEXT BOX BRANCH SEARCH CLICK EVENT
        private void txtBranchSearch_Click(object sender, EventArgs e)
        {//when user enters the form object, instructional text is removed
            if (txtBranchSearch.Text == "Type to search...")
                txtBranchSearch.Clear();
        }

        //TEXT BOX BRANCH SEARCH LEAVE EVENT
        private void txtBranchSearch_Leave(object sender, EventArgs e)
        {//when user leaves searchbox, if it is empty, instructional text is returned. If not empty, searchbox value remains unchanged
            if (txtBranchSearch.Text == "")
            {
                txtBranchSearch.Text = "Type to search...";

                foreach (Branch branch in branchList)
                {
                    lstBranchSearchResults.Items.Add(branch.Nickname + "  ( " + branch.BranchID + " )");
                    lstBranchSearchResults.Sorted = true;
                }
            }
        }

        //LISTBOX BRANCH SEARCH RESULTS MOUSE DOUBLE CLICK EVENT
        private void lstBranchSearchResults_MouseDoubleClick(object sender, MouseEventArgs e)
        {//on double click, same event procedure as select button

            string branch = lstBranchSearchResults.Items[lstBranchSearchResults.SelectedIndex].ToString();

            if (branch != "No matches found...")
            {
                int branchID = Convert.ToInt16(branch.Substring(branch.IndexOf("100"), 5));
                Branch chosenBranch = new Branch();

                foreach (Branch branches in branchList)
                {
                    if (branchID == branches.BranchID)
                        chosenBranch = branches;
                }
                home.Hide();

                frmBranchDetails tempBranchDetails = new frmBranchDetails(chosenBranch);
                tempBranchDetails.Show();
            }
        }

        //LOAD BRANCH LIST METHOD
        public void loadBranchList()
        {//loads brand new branchlist irrespective of search value when called
            lstBranchSearchResults.Items.Clear();

            foreach (Branch branch in branchList)
            {
                lstBranchSearchResults.Items.Add(branch.Nickname + "  ( " + branch.BranchID + " )");
                lstBranchSearchResults.Sorted = true;
            }
        }
    }
}
